﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TTEngine
{
    //一个资源
    public interface IAsset
    {
        UInt32 AssetTypeID
        {
            get;
        }
        bool CanRead
        {
            get;
        }
        bool CanWrite
        {
            get;
        }
    }

    public interface IAssetPacker
    {
        //每一个AssetPacker有一个固定的ID，用字符串取Hash
        UInt32 AssetTypeID
        {
            get;
        }
        bool ReadOnly
        {
            get;
        }
        //如果read完和长度不一样，说明是有坑的
        IAsset Read(System.IO.Stream stream, UInt32 length);
        UInt32 Write(IAsset asset, System.IO.Stream stream);
    }
    interface IPackage : IAsset
    {

    }
    public static class AssetMgr
    {
        public const uint ID_Texture2D = 0xff01;


        static Dictionary<UInt32, IAssetPacker> packers = new Dictionary<uint, IAssetPacker>();
        public static void Reset()
        {
            packers.Clear();
            loadedAsset.Clear();
        }
        public static void RegAssetPacker(IAssetPacker loader)
        {
            if (packers.ContainsKey(loader.AssetTypeID))
                throw new Exception("already have this assetpacker.");
            packers[loader.AssetTypeID] = loader;
        }
        public static IAssetPacker GetAssetPacker(UInt32 assetID)
        {
            return packers[assetID];
        }
        static Dictionary<string, IAsset> loadedAsset = new Dictionary<string, IAsset>();
        //这个根据不同平台，要有不同的实现
        public static void ResetAsset(string path)
        {
            var pathfull = System.IO.Path.GetFullPath(Environment.AssetsPath + "/" + path);
            var key = pathfull.ToLower();
            if (loadedAsset.ContainsKey(key))
            {
                var asset = loadedAsset[key];
                loadedAsset.Remove(key);
            }
        }
        public static IAsset GetAsset(string path)
        {
            var pathfull = System.IO.Path.GetFullPath(Environment.AssetsPath + "/" + path);
            var key = pathfull.ToLower();
            if (loadedAsset.ContainsKey(key))
            {
                return loadedAsset[key];
            }
            else
            {
                var meta = pathfull + ".meta";
                if (System.IO.File.Exists(meta))
                {
                    using (var fs = System.IO.File.OpenRead(meta))
                    {
                        var asset = CreateAsset(fs);
                        loadedAsset[key] = asset;
                        return asset;

                    }
                }
                return null;
                //try to load it
            }
        }
        public static IAsset CreateAsset(System.IO.Stream stream)
        {
            AssetHead head = new AssetHead();
            AssetDataTool.ReadAssetHead(stream, head);

            var key = head.AssetType;
            if (packers.ContainsKey(key) == false)
                throw new Exception("unknown assetKey:" + key.ToString("X08"));

            if (head.AssetDataInfo.Length == 1 && head.AssetDataInfo[0].platform == AssetPlatform.Cross)
            {//这是一个跨平台的资源
                stream.Seek(head.AssetDataInfo[0].dataOffset, System.IO.SeekOrigin.Begin);
                return packers[key].Read(stream, head.AssetDataInfo[0].length);
            }
            for (var i = 0; i < head.AssetDataInfo.Length; i++)
            {
                if (head.AssetDataInfo[i].platform == Environment.AssetPlatform)
                {
                    stream.Seek(head.AssetDataInfo[i].dataOffset, System.IO.SeekOrigin.Begin);
                    return packers[key].Read(stream, head.AssetDataInfo[i].length);
                }
            }
            throw new Exception("error AssetFormat.");
        }
    }
}
